package com.yochi.pictureseditor.effects;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;

import com.yochi.pictureseditor.Processor;
import com.yochi.pictureseditor.R;
import com.yochi.pictureseditor.effects.filters.FilterEnum;
import com.yochi.pictureseditor.effects.filters.filtersimpl.FilterPersonalized;
import com.yochi.pictureseditor.images.Image;
import com.yochi.pictureseditor.tools.Color;
import com.yochi.pictureseditor.tools.IntentUtils;
import com.yochi.pictureseditor.workerthreads.ApplyEffectWorkerThread;

public class EffectApplier {
	
	public static void processEffect(Context context, Image image, EffectEnum effect, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait), 
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes()), true);
			new ApplyEffectWorkerThread(effect, listener).execute(image);
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}

	public static void processEffect(Context context, Image image, FilterEnum filter, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait), 
							context.getResources().getString(R.string.apply_filter)+" "+
							context.getResources().getString(filter.getNameRes()), true);
			new ApplyEffectWorkerThread(filter, listener).execute(image);
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}

	public static void processEffect(Context context, Image image, FilterPersonalized filter, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_filter)+" "+
							context.getResources().getString(FilterEnum.PERSONALIZED.getNameRes()), true);
			new ApplyEffectWorkerThread(filter, listener).execute(image);
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}

	public static void processEffect(Context context, Image image, EffectEnum effect, int parameter, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes()), true);
			new ApplyEffectWorkerThread(effect, parameter, listener).execute(image);   
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}
	
	public static void processEffect(Context context, Image image, EffectEnum effect, int p1, int p2, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes()), true);
			new ApplyEffectWorkerThread(effect, p1, p2, listener).execute(image);   
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}

	public static void processEffect(Context context, Image image, EffectEnum effect, int p1, int p2, int p3, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes()), true);
			new ApplyEffectWorkerThread(effect, p1, p2, p3, listener).execute(image);   
		} else Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
	}

	public static void processEffect(Context context, Image image, EffectEnum effect, ColorEnum color, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes())+
							" ("+context.getResources().getString(color.getNameRes())+")", true);
			new ApplyEffectWorkerThread(effect, color, listener).execute(image);   
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}

	public static void processEffect(Context context, Image image, EffectEnum effect, int parameter, Color color1, Color color2, ApplyEffectListener listener) {
		if(image!=null){
			if(Processor.mProgressDialog.isShowing()) {
				Processor.mProgressDialog.cancel();
			}
			Processor.mProgressDialog = 
					ProgressDialog.show(context, 
							context.getResources().getString(R.string.please_wait),
							context.getResources().getString(R.string.apply_effect)+" "+
							context.getResources().getString(effect.getNameRes()), true);
			new ApplyEffectWorkerThread(effect, parameter, color1, color2, listener).execute(image);   
		} else {
			Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
		}
	}
	
    public static void processCrop(Activity context, boolean imageLoaded, Image image, EffectEnum effect, ApplyEffectListener listener) {
    	if(imageLoaded) {
    		Toast.makeText(context, context.getResources().getString(R.string.apply_effect)+" "+
    												context.getResources().getString(effect.getNameRes()), Toast.LENGTH_SHORT).show();
    		if(effect == EffectEnum.CROP) {
                IntentUtils.cropRectangle(context);
    		} else if(effect == EffectEnum.CROP_CIRCLE) {
    			IntentUtils.cropCircle(context);
    		}
    	} else {
    		Toast.makeText(context, R.string.load_before_apply, Toast.LENGTH_SHORT).show();
    	}
    }
    
    
    
	  //////////////
	 // CALLBACK //
	//////////////
	
	public interface ApplyEffectListener {
		public void effectApplied(EffectEnum applied, Image result);
	}

}
